package com.nuc.bigdata.controller;

import com.nuc.bigdata.orm.dto.NewsCommentsDto;
import com.nuc.bigdata.orm.feign.NewsCommentsFeign;
import com.nuc.bigdata.orm.feign.PhotoNewsCommentsFeign;
import com.nuc.bigdata.service.NewsCommentsService;
import com.nuc.bigdata.service.PhotoNewsCommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * 评论服务模块
 * @author JackLuo
 * @date 2019/11/19
 */
@RestController
@RequestMapping("/oa-comments")
public class NewsCommentsController {
    private final NewsCommentsService commentsService;
    private final PhotoNewsCommentsService photoNewsCommentsService;

    @Autowired
    NewsCommentsController(NewsCommentsService commentsService,
                           PhotoNewsCommentsService photoNewsCommentsService){
        this.commentsService = commentsService;
        this.photoNewsCommentsService = photoNewsCommentsService;
    }

    /**
     * 获取指定新闻id的评论feign的ArrayList
     * @param newsId 新闻唯一标识
     * @param pageSize 页码
     * @return 提供给front服务的新闻评论ArrayList
     */
    @GetMapping("/{newsId}")
    public ArrayList<NewsCommentsFeign> queryCommentsFeign(@PathVariable("newsId")String newsId, @RequestParam Long pageSize){
        ArrayList<NewsCommentsFeign> commentsFeigns = (ArrayList<NewsCommentsFeign>) commentsService.listById(newsId, pageSize);
        return commentsFeigns;
    }

    /**
     * 创建新闻评论
     * @param newsId 新闻唯一标识
     * @param commentsDto 评论类包含userId和content
     */
    @PostMapping("/{newsId}")
    public void createComments(@PathVariable("newsId")String newsId, @RequestBody NewsCommentsDto commentsDto){
        commentsService.createComments(newsId, commentsDto);
    }

    /**
     * 点赞新闻评论
     * @param commentId 评论唯一标识
     * @param userId 用户唯一标识
     */
    @PostMapping("/{commentId}/like")
    public void toCommentLike(@PathVariable("commentId")String commentId,
                              @RequestParam("userId") String userId){
        commentsService.toCommentLike(userId,commentId);
    }

    /**
     * 创建新闻资讯评论
     * @param pnId 新闻资讯唯一标识
     * @param commentsDto 评论类包含userId和content
     */
    @PostMapping("/photos/{pnId}")
    public void createPhotosComments(@PathVariable("pnId")String pnId,
                                     @RequestBody NewsCommentsDto commentsDto) {
        photoNewsCommentsService.createComments(pnId, commentsDto);
    }

    /**
     * 获取指定图片资讯的评论ArrayList
     * @param pnId 图片资讯唯一标识
     * @param pageSize 页码
     * @return 提供给front服务的评论feign的ArrayList
     */
    @GetMapping("/photos/{pnId}")
    public ArrayList<PhotoNewsCommentsFeign> queryPhotosCommentsFeign(@PathVariable("pnId")String pnId,
                                                                      @RequestParam("pageSize") Long pageSize){
        return (ArrayList<PhotoNewsCommentsFeign>) photoNewsCommentsService.listById(pnId, pageSize);
    }

    /**
     * 点赞图片评论
     * @param photoCommentId 图片评论唯一标识
     * @param userId 用户唯一标识
     */
    @PostMapping("/photos/{photoCommentId}/like")
    public void toPhotoCommentLike(@PathVariable("photoCommentId")String photoCommentId,
                                   @RequestParam("userId") String userId){
        photoNewsCommentsService.toCommentLike(userId,photoCommentId );
    }
}
